{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c7cd0529",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fc1eccdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a5157ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_public=pd.read_csv(\"C:/Users/MI/OneDrive/桌面/train_dataset/train_public.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ff88724",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_internet=pd.read_csv(\"C:/Users/MI/OneDrive/桌面/train_dataset/train_internet.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f083dfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_public=pd.read_csv(\"C:/Users/MI/OneDrive/桌面/test_public.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8da5fd1f",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 特征共同项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c9992ab7",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "public_left=set(train_public.columns)-set(train_internet.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "243f8ddc",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'app_type', 'isDefault', 'known_dero', 'known_outstanding_loan'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "public_left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ac16aeb2",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "internet_left=set(train_internet.columns)-set(train_public.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7841ac4b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'f5',\n",
       " 'house_loan_status',\n",
       " 'is_default',\n",
       " 'marriage',\n",
       " 'offsprings',\n",
       " 'sub_class',\n",
       " 'work_type'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "internet_left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dd69c44e",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 发现train_public 的isDefault和train_internet的is_default都属于标签，需同步名称\n",
    "train_internet.rename(columns={'is_default':'isDefault'},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bb289c66",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "common_cols=list(set(train_public.columns)&set(train_internet.columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9c44a2dc",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "public_feature=train_public[common_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a066a441",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "internet_feature=train_internet[common_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "910baabb",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_data=pd.concat([public_feature,internet_feature])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bef7fe13",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "common_cols.remove('isDefault')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "484d1e37",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(common_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8379de67",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_public=test_public[common_cols]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0fbd79c",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 探索数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a256dab",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 自定义基础信息函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6127c149",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "def baseinfo(df):\n",
    "    for col in df.columns:\n",
    "        print(\"列名：\"+col)\n",
    "        print(\"------------\")\n",
    "        print(\"数据类型：\",df[col].dtype)\n",
    "        print(\"-------------------------\")\n",
    "        print(df[col].value_counts())\n",
    "        print(\"------------------------------------\")\n",
    "        print(\"数据值数量\",df[col].nunique())\n",
    "        if df[col].dtype!='object':\n",
    "            print(\"------------------------------------\")\n",
    "            print(\"是否负值\",df[df[col]<0][col].sum())\n",
    "            print(\"------------------------------------\")\n",
    "            print(\"是否有无限值\",np.isinf(df[col]).any())\n",
    "            print(\"------------------------------------\")\n",
    "        print(\"数据缺失值{}\".format(df[col].isna().sum()))\n",
    "        print(\"------------------------------------\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "209d708d",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 匿名特征f0-f5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2cd4492f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：f0\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "4.0     112189\n",
      "5.0     103512\n",
      "3.0     101794\n",
      "6.0      84447\n",
      "2.0      67322\n",
      "7.0      64915\n",
      "8.0      47823\n",
      "9.0      33887\n",
      "1.0      25162\n",
      "10.0     23599\n",
      "11.0     16287\n",
      "12.0     11375\n",
      "13.0      7772\n",
      "14.0      5555\n",
      "15.0      3862\n",
      "0.0       3085\n",
      "16.0      2704\n",
      "17.0      1930\n",
      "18.0      1222\n",
      "19.0      1006\n",
      "20.0       655\n",
      "21.0       441\n",
      "22.0       305\n",
      "23.0       233\n",
      "24.0       162\n",
      "25.0       126\n",
      "26.0        92\n",
      "27.0        59\n",
      "28.0        45\n",
      "29.0        40\n",
      "30.0        23\n",
      "31.0        14\n",
      "32.0        13\n",
      "33.0        11\n",
      "34.0        10\n",
      "36.0         5\n",
      "35.0         5\n",
      "43.0         4\n",
      "39.0         4\n",
      "38.0         3\n",
      "37.0         3\n",
      "44.0         1\n",
      "42.0         1\n",
      "45.0         1\n",
      "Name: f0, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 44\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值38296\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f1\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0    693196\n",
      "1.0       509\n",
      "2.0        24\n",
      "3.0         1\n",
      "4.0         1\n",
      "Name: f1, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值66269\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f2\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "4.0      61377\n",
      "3.0      60994\n",
      "5.0      58753\n",
      "2.0      55125\n",
      "6.0      54357\n",
      "         ...  \n",
      "100.0        1\n",
      "104.0        1\n",
      "90.0         1\n",
      "110.0        1\n",
      "101.0        1\n",
      "Name: f2, Length: 107, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 107\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值38296\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f3\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "11.0     42615\n",
      "10.0     42463\n",
      "9.0      41674\n",
      "12.0     41190\n",
      "8.0      39941\n",
      "         ...  \n",
      "107.0        1\n",
      "87.0         1\n",
      "128.0        1\n",
      "83.0         1\n",
      "96.0         1\n",
      "Name: f3, Length: 102, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 102\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值38297\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f4\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "5.0     72724\n",
      "6.0     71906\n",
      "4.0     67649\n",
      "7.0     67472\n",
      "8.0     61032\n",
      "        ...  \n",
      "59.0        1\n",
      "63.0        1\n",
      "68.0        1\n",
      "57.0        1\n",
      "64.0        1\n",
      "Name: f4, Length: 65, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 65\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值38296\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Train_data[[\"f0\",\"f1\",\"f2\",\"f3\",\"f4\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "512d79b7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：f0\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "4.0     726\n",
      "3.0     655\n",
      "5.0     638\n",
      "6.0     542\n",
      "2.0     474\n",
      "7.0     424\n",
      "8.0     327\n",
      "9.0     222\n",
      "1.0     169\n",
      "10.0    162\n",
      "11.0    111\n",
      "12.0     67\n",
      "13.0     59\n",
      "14.0     38\n",
      "15.0     36\n",
      "16.0     23\n",
      "0.0      21\n",
      "17.0     15\n",
      "19.0      7\n",
      "18.0      7\n",
      "21.0      4\n",
      "22.0      3\n",
      "20.0      3\n",
      "26.0      2\n",
      "27.0      2\n",
      "24.0      2\n",
      "28.0      1\n",
      "23.0      1\n",
      "Name: f0, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 28\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值259\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f1\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0    4542\n",
      "1.0       4\n",
      "Name: f1, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值454\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f2\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "4.0     408\n",
      "3.0     402\n",
      "2.0     384\n",
      "5.0     367\n",
      "6.0     347\n",
      "7.0     329\n",
      "8.0     289\n",
      "9.0     257\n",
      "1.0     250\n",
      "10.0    212\n",
      "11.0    186\n",
      "0.0     159\n",
      "12.0    141\n",
      "14.0    131\n",
      "13.0    119\n",
      "15.0    104\n",
      "16.0     79\n",
      "17.0     79\n",
      "18.0     63\n",
      "19.0     50\n",
      "22.0     44\n",
      "20.0     42\n",
      "23.0     37\n",
      "26.0     27\n",
      "21.0     26\n",
      "30.0     24\n",
      "27.0     21\n",
      "24.0     21\n",
      "28.0     20\n",
      "25.0     19\n",
      "32.0     16\n",
      "29.0     11\n",
      "31.0     10\n",
      "34.0      9\n",
      "35.0      8\n",
      "37.0      7\n",
      "33.0      6\n",
      "39.0      5\n",
      "36.0      5\n",
      "41.0      4\n",
      "52.0      3\n",
      "44.0      3\n",
      "40.0      3\n",
      "49.0      2\n",
      "45.0      2\n",
      "43.0      2\n",
      "42.0      1\n",
      "48.0      1\n",
      "50.0      1\n",
      "46.0      1\n",
      "60.0      1\n",
      "57.0      1\n",
      "53.0      1\n",
      "38.0      1\n",
      "Name: f2, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 54\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值259\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f3\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "13.0    285\n",
      "9.0     274\n",
      "12.0    270\n",
      "11.0    266\n",
      "10.0    264\n",
      "8.0     258\n",
      "14.0    250\n",
      "15.0    225\n",
      "6.0     220\n",
      "7.0     217\n",
      "16.0    211\n",
      "19.0    182\n",
      "17.0    175\n",
      "5.0     172\n",
      "18.0    167\n",
      "21.0    127\n",
      "20.0    122\n",
      "4.0     116\n",
      "23.0    105\n",
      "22.0    100\n",
      "3.0      85\n",
      "25.0     77\n",
      "24.0     77\n",
      "28.0     59\n",
      "26.0     58\n",
      "27.0     49\n",
      "29.0     43\n",
      "30.0     41\n",
      "2.0      31\n",
      "33.0     25\n",
      "34.0     22\n",
      "31.0     21\n",
      "35.0     19\n",
      "32.0     18\n",
      "36.0     16\n",
      "38.0     14\n",
      "37.0     12\n",
      "39.0      9\n",
      "40.0      8\n",
      "44.0      7\n",
      "42.0      6\n",
      "48.0      6\n",
      "41.0      5\n",
      "43.0      5\n",
      "46.0      4\n",
      "45.0      4\n",
      "49.0      3\n",
      "47.0      3\n",
      "51.0      2\n",
      "50.0      2\n",
      "60.0      1\n",
      "58.0      1\n",
      "62.0      1\n",
      "54.0      1\n",
      "Name: f3, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 54\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值259\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：f4\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "5.0     492\n",
      "6.0     467\n",
      "4.0     434\n",
      "8.0     432\n",
      "7.0     419\n",
      "3.0     377\n",
      "9.0     360\n",
      "10.0    302\n",
      "11.0    219\n",
      "2.0     182\n",
      "13.0    177\n",
      "12.0    173\n",
      "14.0    133\n",
      "15.0    102\n",
      "1.0      89\n",
      "16.0     85\n",
      "17.0     69\n",
      "19.0     44\n",
      "18.0     40\n",
      "21.0     30\n",
      "20.0     25\n",
      "22.0     18\n",
      "0.0      15\n",
      "23.0     11\n",
      "25.0     10\n",
      "24.0      7\n",
      "27.0      6\n",
      "29.0      5\n",
      "26.0      4\n",
      "28.0      4\n",
      "32.0      3\n",
      "38.0      2\n",
      "34.0      1\n",
      "31.0      1\n",
      "33.0      1\n",
      "30.0      1\n",
      "36.0      1\n",
      "Name: f4, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 37\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值259\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Test_public[[\"f0\",\"f1\",\"f2\",\"f3\",\"f4\"]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7588dd6",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2beb479f",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：user_id\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "225589    2\n",
      "201492    2\n",
      "243484    2\n",
      "213247    2\n",
      "218572    2\n",
      "         ..\n",
      "446066    1\n",
      "444019    1\n",
      "458356    1\n",
      "456309    1\n",
      "2047      1\n",
      "Name: user_id, Length: 750000, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 750000\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：house_exist\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0    375860\n",
      "1    301920\n",
      "2     81936\n",
      "3       177\n",
      "5        77\n",
      "4        30\n",
      "Name: house_exist, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 6\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：censor_status\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "1    294222\n",
      "2    236616\n",
      "0    229162\n",
      "Name: censor_status, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：industry\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "金融业                121940\n",
      "电力、热力生产供应业          91460\n",
      "公共服务、社会组织           76769\n",
      "住宿和餐饮业              67993\n",
      "文化和体育业              61190\n",
      "信息传输、软件和信息技术服务业     60604\n",
      "建筑业                 53016\n",
      "房地产业                45448\n",
      "交通运输、仓储和邮政业         38017\n",
      "农、林、牧、渔业            37873\n",
      "采矿业                 37815\n",
      "国际组织                22765\n",
      "批发和零售业              22579\n",
      "制造业                 22531\n",
      "Name: industry, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 14\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：work_year\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "10+ years    249596\n",
      "2 years       68835\n",
      "< 1 year      60963\n",
      "3 years       60904\n",
      "1 year        49875\n",
      "5 years       47650\n",
      "4 years       45599\n",
      "6 years       35386\n",
      "8 years       34315\n",
      "7 years       33636\n",
      "9 years       28772\n",
      "Name: work_year, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 11\n",
      "数据缺失值44469\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：employer_type\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "普通企业       345140\n",
      "政府机构       196162\n",
      "幼教与中小学校     76044\n",
      "上市企业        75926\n",
      "世界五百强       41080\n",
      "高等教育机构      25648\n",
      "Name: employer_type, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 6\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Train_data[[\"user_id\",\"house_exist\",\"censor_status\",\"industry\",\"work_year\",\"employer_type\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8d4af561",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：user_id\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "235760    1\n",
      "232727    1\n",
      "240931    1\n",
      "214306    1\n",
      "224764    1\n",
      "         ..\n",
      "242303    1\n",
      "220766    1\n",
      "234107    1\n",
      "240467    1\n",
      "215039    1\n",
      "Name: user_id, Length: 5000, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5000\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：house_exist\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0    2448\n",
      "1    2026\n",
      "2     524\n",
      "5       1\n",
      "3       1\n",
      "Name: house_exist, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：censor_status\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "1    1946\n",
      "0    1532\n",
      "2    1522\n",
      "Name: censor_status, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：industry\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "金融业                818\n",
      "电力、热力生产供应业         612\n",
      "公共服务、社会组织          557\n",
      "住宿和餐饮业             429\n",
      "信息传输、软件和信息技术服务业    419\n",
      "文化和体育业             358\n",
      "建筑业                314\n",
      "房地产业               285\n",
      "交通运输、仓储和邮政业        283\n",
      "采矿业                277\n",
      "农、林、牧、渔业           218\n",
      "批发和零售业             159\n",
      "国际组织               144\n",
      "制造业                127\n",
      "Name: industry, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 14\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：work_year\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "10+ years    1630\n",
      "2 years       463\n",
      "3 years       385\n",
      "< 1 year      382\n",
      "1 year        344\n",
      "5 years       334\n",
      "4 years       322\n",
      "6 years       230\n",
      "7 years       224\n",
      "8 years       224\n",
      "9 years       184\n",
      "Name: work_year, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 11\n",
      "数据缺失值278\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：employer_type\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "普通企业       2264\n",
      "政府机构       1281\n",
      "幼教与中小学校     503\n",
      "上市企业        499\n",
      "世界五百强       281\n",
      "高等教育机构      172\n",
      "Name: employer_type, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 6\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Test_public[[\"user_id\",\"house_exist\",\"censor_status\",\"industry\",\"work_year\",\"employer_type\"]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e1478d0",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 基本贷款信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8d68e0e0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：loan_id\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0         1\n",
      "539218    1\n",
      "582217    1\n",
      "588362    1\n",
      "586315    1\n",
      "         ..\n",
      "775632    1\n",
      "777681    1\n",
      "771538    1\n",
      "773587    1\n",
      "2047      1\n",
      "Name: loan_id, Length: 760000, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 760000\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：use\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0     440890\n",
      "4     166631\n",
      "2      49540\n",
      "5      43995\n",
      "3      16677\n",
      "9       8777\n",
      "1       8648\n",
      "8       8226\n",
      "10      5352\n",
      "7       5116\n",
      "6       4135\n",
      "12      1295\n",
      "11       532\n",
      "13       186\n",
      "Name: use, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 14\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：year_of_loan\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "3    576511\n",
      "5    183489\n",
      "Name: year_of_loan, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：issue_date\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "2016-03-01    27230\n",
      "2015-10-01    23885\n",
      "2015-07-01    23016\n",
      "2015-12-01    21727\n",
      "2014-10-01    20136\n",
      "              ...  \n",
      "2007/10/1         1\n",
      "2007-06-01        1\n",
      "2009/8/1          1\n",
      "2007/12/1         1\n",
      "2008/12/1         1\n",
      "Name: issue_date, Length: 266, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 266\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：earlies_credit_mon\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "Aug-2001    5215\n",
      "Aug-2002    5065\n",
      "Sep-2003    5050\n",
      "Oct-2001    4925\n",
      "Sep-2004    4909\n",
      "            ... \n",
      "Oct-69         1\n",
      "Jan-70         1\n",
      "Jan-72         1\n",
      "Aug-1955       1\n",
      "Dec-72         1\n",
      "Name: earlies_credit_mon, Length: 1246, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1246\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：post_code\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "134.0    8506\n",
      "19.0     8155\n",
      "51.0     7761\n",
      "31.0     6972\n",
      "4.0      6812\n",
      "         ... \n",
      "915.0       1\n",
      "940.0       1\n",
      "928.0       1\n",
      "921.0       1\n",
      "932.0       1\n",
      "Name: post_code, Length: 930, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 930\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值1\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：region\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "8     111011\n",
      "14     62482\n",
      "13     61827\n",
      "21     53973\n",
      "2      29011\n",
      "30     27191\n",
      "0      25803\n",
      "19     24909\n",
      "3      24479\n",
      "9      21757\n",
      "7      21384\n",
      "23     19920\n",
      "10     18641\n",
      "12     17599\n",
      "26     17479\n",
      "22     16855\n",
      "18     16432\n",
      "4      13460\n",
      "11     12305\n",
      "24     12156\n",
      "32     11439\n",
      "38     11397\n",
      "36     11060\n",
      "27     10015\n",
      "17      9361\n",
      "35      9157\n",
      "5       9093\n",
      "20      8634\n",
      "43      7291\n",
      "42      6914\n",
      "15      6328\n",
      "37      5709\n",
      "45      5612\n",
      "16      4076\n",
      "28      3860\n",
      "44      3775\n",
      "33      3623\n",
      "6       3324\n",
      "39      2798\n",
      "40      2177\n",
      "31      2157\n",
      "34      2029\n",
      "25      1995\n",
      "48      1791\n",
      "41      1682\n",
      "1       1536\n",
      "29      1469\n",
      "47      1153\n",
      "49       965\n",
      "46       901\n",
      "50         5\n",
      "Name: region, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 51\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：title\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0        373659\n",
      "4.0        140791\n",
      "5.0         42517\n",
      "6.0         37780\n",
      "3.0         13679\n",
      "            ...  \n",
      "19306.0         1\n",
      "60416.0         1\n",
      "7960.0          1\n",
      "52527.0         1\n",
      "30666.0         1\n",
      "Name: title, Length: 37941, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 37941\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值1\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：policy_code\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "1.0    760000\n",
      "Name: policy_code, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：class\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "B    222089\n",
      "C    215676\n",
      "A    132583\n",
      "D    113504\n",
      "E     52938\n",
      "F     18102\n",
      "G      5108\n",
      "Name: class, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 7\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Train_data[[\"loan_id\",\"use\",\"year_of_loan\",\"issue_date\",\"earlies_credit_mon\",\n",
    "                     \"post_code\",\"region\",\"title\",\"policy_code\",\"class\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "3ff8603f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：loan_id\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "1048576    1\n",
      "1049946    1\n",
      "1043811    1\n",
      "1045747    1\n",
      "1023329    1\n",
      "          ..\n",
      "1004469    1\n",
      "1012347    1\n",
      "1020040    1\n",
      "1049209    1\n",
      "1046525    1\n",
      "Name: loan_id, Length: 5000, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5000\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：use\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0     2841\n",
      "4     1111\n",
      "2      352\n",
      "5      282\n",
      "3      116\n",
      "9       73\n",
      "1       56\n",
      "8       45\n",
      "10      44\n",
      "6       38\n",
      "7       27\n",
      "12       9\n",
      "13       3\n",
      "11       3\n",
      "Name: use, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 14\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：year_of_loan\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "3    3769\n",
      "5    1231\n",
      "Name: year_of_loan, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：issue_date\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "2015/10/1    174\n",
      "2016/3/1     172\n",
      "2015/7/1     160\n",
      "2015/12/1    157\n",
      "2014/10/1    152\n",
      "            ... \n",
      "2008/10/1      1\n",
      "2008/6/1       1\n",
      "2009/10/1      1\n",
      "2008/1/1       1\n",
      "2009/5/1       1\n",
      "Name: issue_date, Length: 123, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 123\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：earlies_credit_mon\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "Oct-99    39\n",
      "1-Aug     39\n",
      "3-Sep     38\n",
      "1-Dec     36\n",
      "2-Apr     34\n",
      "          ..\n",
      "Feb-70     1\n",
      "Feb-82     1\n",
      "Jun-68     1\n",
      "Jun-71     1\n",
      "Dec-79     1\n",
      "Name: earlies_credit_mon, Length: 476, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 476\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：post_code\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "19     62\n",
      "134    61\n",
      "31     52\n",
      "51     48\n",
      "4      46\n",
      "       ..\n",
      "669     1\n",
      "295     1\n",
      "661     1\n",
      "303     1\n",
      "855     1\n",
      "Name: post_code, Length: 730, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 730\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：region\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "8     762\n",
      "14    408\n",
      "13    399\n",
      "21    302\n",
      "30    192\n",
      "0     189\n",
      "2     185\n",
      "19    163\n",
      "3     157\n",
      "9     150\n",
      "7     141\n",
      "26    131\n",
      "12    119\n",
      "10    111\n",
      "23    110\n",
      "22    108\n",
      "18    105\n",
      "4     104\n",
      "24     76\n",
      "11     74\n",
      "32     74\n",
      "35     70\n",
      "20     69\n",
      "38     68\n",
      "5      67\n",
      "36     67\n",
      "17     54\n",
      "43     54\n",
      "27     50\n",
      "37     49\n",
      "15     49\n",
      "42     47\n",
      "45     44\n",
      "16     36\n",
      "33     26\n",
      "44     23\n",
      "28     22\n",
      "34     16\n",
      "39     16\n",
      "41     15\n",
      "25     15\n",
      "6      13\n",
      "31     13\n",
      "48     11\n",
      "40     10\n",
      "1       9\n",
      "46      8\n",
      "29      8\n",
      "47      7\n",
      "49      4\n",
      "Name: region, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 50\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：title\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0        2404\n",
      "4         948\n",
      "5         304\n",
      "6         233\n",
      "3         102\n",
      "         ... \n",
      "17001       1\n",
      "25189       1\n",
      "605         1\n",
      "601         1\n",
      "26611       1\n",
      "Name: title, Length: 508, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 508\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：policy_code\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "1    5000\n",
      "Name: policy_code, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：class\n",
      "------------\n",
      "数据类型： object\n",
      "-------------------------\n",
      "B    1440\n",
      "C    1438\n",
      "A     886\n",
      "D     763\n",
      "E     338\n",
      "F     112\n",
      "G      23\n",
      "Name: class, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 7\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Test_public[[\"loan_id\",\"use\",\"year_of_loan\",\"issue_date\",\"earlies_credit_mon\",\n",
    "                     \"post_code\",\"region\",\"title\",\"policy_code\",\"class\"]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2db69a8",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 贷款信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "abdb8816",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：recircle_b\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.00000         3774\n",
      "4784.00000        68\n",
      "6325.00000        65\n",
      "6018.00000        62\n",
      "5235.00000        62\n",
      "                ... \n",
      "251169.00000       1\n",
      "41566.00000        1\n",
      "41764.61538        1\n",
      "147716.00000       1\n",
      "356009.00000       1\n",
      "Name: recircle_b, Length: 76403, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 76403\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：recircle_u\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.000000      3995\n",
      "57.000000     1499\n",
      "61.000000     1494\n",
      "53.000000     1490\n",
      "55.000000     1485\n",
      "              ... \n",
      "115.961539       1\n",
      "81.953846        1\n",
      "100.384615       1\n",
      "98.307692        1\n",
      "5.790000         1\n",
      "Name: recircle_u, Length: 3445, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3445\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值489\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：debt_loan_ratio\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "19.200000     566\n",
      "18.000000     561\n",
      "13.200000     545\n",
      "16.800000     533\n",
      "14.400000     517\n",
      "             ... \n",
      "53.570000       1\n",
      "15.390909       1\n",
      "102.540000      1\n",
      "15.889091       1\n",
      "1.516364        1\n",
      "Name: debt_loan_ratio, Length: 10514, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 10514\n",
      "------------------------------------\n",
      "是否负值 -2.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值230\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：total_loan\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "10000.000000    55507\n",
      "12000.000000    40779\n",
      "20000.000000    39451\n",
      "15000.000000    39161\n",
      "35000.000000    28594\n",
      "                ...  \n",
      "12477.272730        1\n",
      "4659.090909         1\n",
      "36450.000000        1\n",
      "18204.545450        1\n",
      "25204.545450        1\n",
      "Name: total_loan, Length: 2735, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2735\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：monthly_payment\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "327.34     1764\n",
      "301.15     1510\n",
      "332.10     1456\n",
      "318.79     1284\n",
      "491.01     1198\n",
      "           ... \n",
      "919.91        1\n",
      "257.51        1\n",
      "563.20        1\n",
      "480.23        1\n",
      "1127.40       1\n",
      "Name: monthly_payment, Length: 71244, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 71244\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0     694980\n",
      "3       4777\n",
      "2       3166\n",
      "1       3052\n",
      "5       2299\n",
      "4       2205\n",
      "14      2144\n",
      "13      2140\n",
      "22      2128\n",
      "19      2108\n",
      "20      2103\n",
      "26      2096\n",
      "8       2090\n",
      "24      2082\n",
      "25      2075\n",
      "27      2069\n",
      "18      2065\n",
      "16      2063\n",
      "10      2061\n",
      "6       2050\n",
      "7       2045\n",
      "29      2043\n",
      "23      2038\n",
      "9       2038\n",
      "17      2027\n",
      "21      2022\n",
      "28      2019\n",
      "15      2019\n",
      "11      2007\n",
      "12      1989\n",
      "Name: early_return, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 30\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return_amount\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0        693940\n",
      "240         194\n",
      "336         183\n",
      "360         177\n",
      "720         172\n",
      "          ...  \n",
      "4691          1\n",
      "6738          1\n",
      "593           1\n",
      "11861         1\n",
      "3830          1\n",
      "Name: early_return_amount, Length: 4901, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 4901\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return_amount_3mon\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.000000       695446\n",
      "126.600000          5\n",
      "60.600000           5\n",
      "981.000000          4\n",
      "296.400000          4\n",
      "                ...  \n",
      "414.940743          1\n",
      "1857.171263         1\n",
      "195.601052          1\n",
      "591.806462          1\n",
      "633.041824          1\n",
      "Name: early_return_amount_3mon, Length: 63735, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 63735\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：interest\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "10.990    21290\n",
      "11.990    18533\n",
      "13.990    16430\n",
      "5.320     16041\n",
      "9.170     14581\n",
      "          ...  \n",
      "25.608        1\n",
      "18.530        1\n",
      "10.373        1\n",
      "12.465        1\n",
      "10.008        1\n",
      "Name: interest, Length: 1318, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1318\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Train_data[[\"recircle_b\",\"recircle_u\",\"debt_loan_ratio\",\"total_loan\",\"monthly_payment\",\n",
    "                     \"early_return\",\"early_return_amount\",\"early_return_amount_3mon\",\"interest\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "034280a0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：recircle_b\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.000000        22\n",
      "15854.461540     3\n",
      "13650.000000     3\n",
      "13851.923080     3\n",
      "2746.153846      3\n",
      "                ..\n",
      "6641.384615      1\n",
      "30970.153850     1\n",
      "9646.000000      1\n",
      "10809.230770     1\n",
      "13523.000000     1\n",
      "Name: recircle_b, Length: 4832, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 4832\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：recircle_u\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.000000      30\n",
      "76.246154     11\n",
      "48.461538     11\n",
      "56.400000      9\n",
      "72.000000      9\n",
      "              ..\n",
      "110.769231     1\n",
      "42.500000      1\n",
      "92.192308      1\n",
      "32.200000      1\n",
      "94.123077      1\n",
      "Name: recircle_u, Length: 2450, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2450\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：debt_loan_ratio\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "13.090909    7\n",
      "13.647273    6\n",
      "13.581818    5\n",
      "18.654545    5\n",
      "22.254545    5\n",
      "            ..\n",
      "29.600000    1\n",
      "25.540000    1\n",
      "6.440000     1\n",
      "33.305455    1\n",
      "14.465455    1\n",
      "Name: debt_loan_ratio, Length: 3836, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3836\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：total_loan\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "16363.636360    139\n",
      "10909.090910    112\n",
      "8181.818182      92\n",
      "6545.454545      90\n",
      "13090.909090     87\n",
      "               ... \n",
      "24675.000000      1\n",
      "30500.000000      1\n",
      "23550.000000      1\n",
      "7690.909091       1\n",
      "15109.090910      1\n",
      "Name: total_loan, Length: 1061, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 1061\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：monthly_payment\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "392.81    15\n",
      "327.34    13\n",
      "332.10    10\n",
      "361.38    10\n",
      "301.15    10\n",
      "          ..\n",
      "32.24      1\n",
      "126.20     1\n",
      "408.46     1\n",
      "128.04     1\n",
      "238.17     1\n",
      "Name: monthly_payment, Length: 3722, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 3722\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0    1900\n",
      "3    1611\n",
      "2     634\n",
      "1     605\n",
      "5     144\n",
      "4     106\n",
      "Name: early_return, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 6\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return_amount\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0       1861\n",
      "569        5\n",
      "730        4\n",
      "2617       4\n",
      "712        4\n",
      "        ... \n",
      "877        1\n",
      "2928       1\n",
      "889        1\n",
      "2944       1\n",
      "2047       1\n",
      "Name: early_return_amount, Length: 2566, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2566\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：early_return_amount_3mon\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.000000       2623\n",
      "473.538461        3\n",
      "281.076923        3\n",
      "111.323077        3\n",
      "360.230769        3\n",
      "               ... \n",
      "2370.276923       1\n",
      "1711.384615       1\n",
      "28.000000         1\n",
      "901.038462        1\n",
      "435.507692        1\n",
      "Name: early_return_amount_3mon, Length: 2294, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2294\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：interest\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "15.389    50\n",
      "12.089    50\n",
      "10.990    45\n",
      "11.990    44\n",
      "9.891     43\n",
      "          ..\n",
      "17.490     1\n",
      "13.230     1\n",
      "18.390     1\n",
      "17.721     1\n",
      "7.119      1\n",
      "Name: interest, Length: 834, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 834\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Test_public[[\"recircle_b\",\"recircle_u\",\"debt_loan_ratio\",\"total_loan\",\"monthly_payment\",\n",
    "                     \"early_return\",\"early_return_amount\",\"early_return_amount_3mon\",\"interest\"]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d93a6bd",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 用户信用信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9dab7085",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：scoring_high\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "664.000000     67466\n",
      "674.000000     65559\n",
      "669.000000     65241\n",
      "679.000000     58196\n",
      "684.000000     57375\n",
      "               ...  \n",
      "888.333333         1\n",
      "864.393939         1\n",
      "843.181818         1\n",
      "823.295455         1\n",
      "1018.750000        1\n",
      "Name: scoring_high, Length: 399, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 399\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：scoring_low\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "660.000000    67694\n",
      "670.000000    65787\n",
      "665.000000    65458\n",
      "675.000000    58391\n",
      "680.000000    57553\n",
      "              ...  \n",
      "910.909091        1\n",
      "670.909091        1\n",
      "745.454545        1\n",
      "691.363636        1\n",
      "894.545455        1\n",
      "Name: scoring_low, Length: 134, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 134\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：del_in_18month\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0     613429\n",
      "1.0      97429\n",
      "2.0      28446\n",
      "3.0      10394\n",
      "4.0       4575\n",
      "5.0       2382\n",
      "6.0       1325\n",
      "7.0        730\n",
      "8.0        421\n",
      "9.0        276\n",
      "10.0       186\n",
      "11.0       137\n",
      "12.0        82\n",
      "14.0        50\n",
      "13.0        48\n",
      "15.0        23\n",
      "16.0        19\n",
      "18.0        12\n",
      "19.0         9\n",
      "20.0         7\n",
      "17.0         5\n",
      "21.0         3\n",
      "22.0         3\n",
      "26.0         2\n",
      "29.0         2\n",
      "25.0         1\n",
      "24.0         1\n",
      "27.0         1\n",
      "39.0         1\n",
      "30.0         1\n",
      "Name: del_in_18month, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 30\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：initial_list_status\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0    442975\n",
      "1    317025\n",
      "Name: initial_list_status, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：pub_dero_bankrup\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0     665167\n",
      "1.0      88853\n",
      "2.0       4355\n",
      "3.0        892\n",
      "4.0        232\n",
      "5.0         78\n",
      "6.0         23\n",
      "7.0         11\n",
      "8.0          3\n",
      "9.0          3\n",
      "12.0         1\n",
      "Name: pub_dero_bankrup, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 11\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值382\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Train_data[[\"scoring_high\",\"scoring_low\",\"del_in_18month\",\"initial_list_status\",\"pub_dero_bankrup\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "65c67406",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列名：scoring_high\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "900.000000     59\n",
      "589.431818     51\n",
      "758.333333     51\n",
      "715.909091     51\n",
      "840.000000     50\n",
      "               ..\n",
      "909.090909      1\n",
      "866.666667      1\n",
      "843.181818      1\n",
      "1125.000000     1\n",
      "700.227273      1\n",
      "Name: scoring_high, Length: 332, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 332\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：scoring_low\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "720.000000    157\n",
      "544.090909    129\n",
      "600.000000    111\n",
      "725.454545    110\n",
      "675.000000    109\n",
      "             ... \n",
      "722.727273      1\n",
      "820.000000      1\n",
      "825.000000      1\n",
      "800.000000      1\n",
      "679.090909      1\n",
      "Name: scoring_low, Length: 128, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 128\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：del_in_18month\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0     4072\n",
      "1      615\n",
      "2      191\n",
      "3       62\n",
      "4       24\n",
      "5       18\n",
      "6        8\n",
      "7        3\n",
      "8        2\n",
      "9        2\n",
      "12       1\n",
      "16       1\n",
      "14       1\n",
      "Name: del_in_18month, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 13\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：initial_list_status\n",
      "------------\n",
      "数据类型： int64\n",
      "-------------------------\n",
      "0    2963\n",
      "1    2037\n",
      "Name: initial_list_status, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 2\n",
      "------------------------------------\n",
      "是否负值 0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值0\n",
      "------------------------------------\n",
      "\n",
      "\n",
      "列名：pub_dero_bankrup\n",
      "------------\n",
      "数据类型： float64\n",
      "-------------------------\n",
      "0.0    4341\n",
      "1.0     613\n",
      "2.0      28\n",
      "3.0      11\n",
      "4.0       3\n",
      "Name: pub_dero_bankrup, dtype: int64\n",
      "------------------------------------\n",
      "数据值数量 5\n",
      "------------------------------------\n",
      "是否负值 0.0\n",
      "------------------------------------\n",
      "是否有无限值 False\n",
      "------------------------------------\n",
      "数据缺失值4\n",
      "------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseinfo(Test_public[[\"scoring_high\",\"scoring_low\",\"del_in_18month\",\"initial_list_status\",\"pub_dero_bankrup\"]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7064d2c3",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70ae7292",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 删除异常值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "878ebf92",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 删除post_code邮政编码前三位为空的值\n",
    "Train_middle=Train_data[~Train_data[\"post_code\"].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2879cc10",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>649.090909</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>7734.230769</td>\n",
       "      <td>9927</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>893.750000</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>31329.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>903.636364</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>18514.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>875.000000</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>20707.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>710.606061</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>14016.153850</td>\n",
       "      <td>0</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749995</th>\n",
       "      <td>22.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Jun-1995</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>669.000000</td>\n",
       "      <td>上市企业</td>\n",
       "      <td>9572.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>62.200000</td>\n",
       "      <td>11.470</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>95.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749996</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Sep-2001</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8 years</td>\n",
       "      <td>4.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>774.000000</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>14183.000000</td>\n",
       "      <td>810</td>\n",
       "      <td>30.500000</td>\n",
       "      <td>6.030</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>74.0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749997</th>\n",
       "      <td>11.0</td>\n",
       "      <td>7</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-2008</td>\n",
       "      <td>6.0</td>\n",
       "      <td>8 years</td>\n",
       "      <td>8.0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>669.000000</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>9259.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>72.900000</td>\n",
       "      <td>15.410</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>74.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749998</th>\n",
       "      <td>11.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-2006</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>679.000000</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>9825.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>71.200000</td>\n",
       "      <td>9.440</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>134.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>749999</th>\n",
       "      <td>13.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Feb-2001</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>6.0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>699.000000</td>\n",
       "      <td>幼教与中小学校</td>\n",
       "      <td>9019.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>78.400000</td>\n",
       "      <td>19.220</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>616.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>759999 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          f3  use  policy_code earlies_credit_mon    f0  work_year    title  \\\n",
       "0        5.0    2          1.0              1-Dec   1.0    3 years      5.0   \n",
       "1       45.0    0          1.0             Apr-90   7.0  10+ years  40642.0   \n",
       "2       28.0    4          1.0             Oct-91   6.0  10+ years    154.0   \n",
       "3       15.0    0          1.0              1-Jun   5.0    6 years      0.0   \n",
       "4       15.0    0          1.0              2-May  10.0   < 1 year      0.0   \n",
       "...      ...  ...          ...                ...   ...        ...      ...   \n",
       "749995  22.0    0          1.0           Jun-1995   6.0    4 years      0.0   \n",
       "749996   4.0    4          1.0           Sep-2001   2.0    8 years      4.0   \n",
       "749997  11.0    7          1.0           Oct-2008   6.0    8 years      8.0   \n",
       "749998  11.0    0          1.0           Apr-2006   7.0  10+ years      0.0   \n",
       "749999  13.0    5          1.0           Feb-2001   7.0    3 years      6.0   \n",
       "\n",
       "       class   f1  pub_dero_bankrup  ...  scoring_high  employer_type  \\\n",
       "0          C  0.0               0.0  ...    649.090909           政府机构   \n",
       "1          C  0.0               0.0  ...    893.750000           政府机构   \n",
       "2          A  0.0               0.0  ...    903.636364           政府机构   \n",
       "3          A  0.0               0.0  ...    875.000000          世界五百强   \n",
       "4          C  0.0               0.0  ...    710.606061           政府机构   \n",
       "...      ...  ...               ...  ...           ...            ...   \n",
       "749995     B  0.0               0.0  ...    669.000000           上市企业   \n",
       "749996     A  0.0               0.0  ...    774.000000           政府机构   \n",
       "749997     D  0.0               0.0  ...    669.000000           政府机构   \n",
       "749998     B  0.0               1.0  ...    679.000000           政府机构   \n",
       "749999     D  0.0               0.0  ...    699.000000        幼教与中小学校   \n",
       "\n",
       "          recircle_b  early_return_amount  recircle_u  interest  \\\n",
       "0        7734.230769                 9927   91.800000    11.466   \n",
       "1       31329.000000                    0   54.800000    16.841   \n",
       "2       18514.000000                    0   57.692308     8.900   \n",
       "3       20707.000000                    0   42.600000     4.788   \n",
       "4       14016.153850                    0   30.461538    12.790   \n",
       "...              ...                  ...         ...       ...   \n",
       "749995   9572.000000                    0   62.200000    11.470   \n",
       "749996  14183.000000                  810   30.500000     6.030   \n",
       "749997   9259.000000                    0   72.900000    15.410   \n",
       "749998   9825.000000                    0   71.200000     9.440   \n",
       "749999   9019.000000                    0   78.400000    19.220   \n",
       "\n",
       "               industry  post_code  early_return  year_of_loan  \n",
       "0                   金融业      193.0             3             3  \n",
       "1                   金融业      491.0             0             5  \n",
       "2             公共服务、社会组织      459.0             0             3  \n",
       "3                文化和体育业      157.0             0             3  \n",
       "4       信息传输、软件和信息技术服务业       38.0             0             3  \n",
       "...                 ...        ...           ...           ...  \n",
       "749995           文化和体育业       95.0             0             3  \n",
       "749996           住宿和餐饮业       74.0             6             3  \n",
       "749997           住宿和餐饮业       74.0             0             3  \n",
       "749998  信息传输、软件和信息技术服务业      134.0             0             3  \n",
       "749999           文化和体育业      616.0             0             5  \n",
       "\n",
       "[759999 rows x 36 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d921e098",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#删除title借款人提供的贷款名称为空的值\n",
    "Train_middle=Train_middle[~(Train_middle['title'].isnull())]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "21f7453f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(759998, 36)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1d177fae",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#删除debt_loan_ratio债务收入比为负的值\n",
    "Train_middle=Train_middle[Train_middle['debt_loan_ratio']>=0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5d824066",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#删除early_return（借款人提前还款次数）<=0,但是early_return_amount（贷款人提前还款累积金额）>0的异常值\n",
    "cond1=((Train_middle[\"early_return\"]<=0)&(Train_middle[\"early_return_amount\"]>0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bb8d2c25",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle=Train_middle[~cond1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8b10caee",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(758726, 36)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf0bf05f",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 缺失值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b6ae825d",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "mode_cols=[\"work_year\"]\n",
    "median_cols=[\"recircle_u\",\"pub_dero_bankrup\",\"f0\",\"f1\",\"f2\",\"f3\",\"f4\"]\n",
    "def fillna(df,median_cols=median_cols,mode_cols=mode_cols):\n",
    "    #中位数填充\n",
    "    median_value=df[median_cols].median()\n",
    "    med_dict=dict(zip(median_value.index,median_value))\n",
    "    df=df.fillna(med_dict)\n",
    "    \n",
    "    #众数填充\n",
    "    mode_value=df[mode_cols].mode().loc[0]\n",
    "    mod_dict=dict(zip(mode_value.index,mode_value))\n",
    "    df=df.fillna(mod_dict)\n",
    "    \n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7f88ac0a",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>649.090909</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>7734.230769</td>\n",
       "      <td>9927</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>893.750000</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>31329.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>903.636364</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>18514.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>875.000000</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>20707.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>710.606061</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>14016.153850</td>\n",
       "      <td>0</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  scoring_high  employer_type    recircle_b  \\\n",
       "0  0.0               0.0  ...    649.090909           政府机构   7734.230769   \n",
       "1  0.0               0.0  ...    893.750000           政府机构  31329.000000   \n",
       "2  0.0               0.0  ...    903.636364           政府机构  18514.000000   \n",
       "3  0.0               0.0  ...    875.000000          世界五百强  20707.000000   \n",
       "4  0.0               0.0  ...    710.606061           政府机构  14016.153850   \n",
       "\n",
       "   early_return_amount  recircle_u  interest         industry  post_code  \\\n",
       "0                 9927   91.800000    11.466              金融业      193.0   \n",
       "1                    0   54.800000    16.841              金融业      491.0   \n",
       "2                    0   57.692308     8.900        公共服务、社会组织      459.0   \n",
       "3                    0   42.600000     4.788           文化和体育业      157.0   \n",
       "4                    0   30.461538    12.790  信息传输、软件和信息技术服务业       38.0   \n",
       "\n",
       "   early_return  year_of_loan  \n",
       "0             3             3  \n",
       "1             0             5  \n",
       "2             0             3  \n",
       "3             0             3  \n",
       "4             0             3  \n",
       "\n",
       "[5 rows x 36 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7aed3258",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle=fillna(Train_middle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1664f69f",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "f3                          0\n",
       "use                         0\n",
       "policy_code                 0\n",
       "earlies_credit_mon          0\n",
       "f0                          0\n",
       "work_year                   0\n",
       "title                       0\n",
       "class                       0\n",
       "f1                          0\n",
       "pub_dero_bankrup            0\n",
       "monthly_payment             0\n",
       "scoring_low                 0\n",
       "f4                          0\n",
       "early_return_amount_3mon    0\n",
       "total_loan                  0\n",
       "isDefault                   0\n",
       "issue_date                  0\n",
       "house_exist                 0\n",
       "del_in_18month              0\n",
       "user_id                     0\n",
       "loan_id                     0\n",
       "f2                          0\n",
       "initial_list_status         0\n",
       "censor_status               0\n",
       "region                      0\n",
       "debt_loan_ratio             0\n",
       "scoring_high                0\n",
       "employer_type               0\n",
       "recircle_b                  0\n",
       "early_return_amount         0\n",
       "recircle_u                  0\n",
       "interest                    0\n",
       "industry                    0\n",
       "post_code                   0\n",
       "early_return                0\n",
       "year_of_loan                0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7b04a892",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_middle=fillna(Test_public)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "abbe24be",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "f3                          0\n",
       "use                         0\n",
       "policy_code                 0\n",
       "earlies_credit_mon          0\n",
       "f0                          0\n",
       "work_year                   0\n",
       "title                       0\n",
       "class                       0\n",
       "f1                          0\n",
       "pub_dero_bankrup            0\n",
       "monthly_payment             0\n",
       "scoring_low                 0\n",
       "f4                          0\n",
       "early_return_amount_3mon    0\n",
       "total_loan                  0\n",
       "issue_date                  0\n",
       "house_exist                 0\n",
       "del_in_18month              0\n",
       "user_id                     0\n",
       "loan_id                     0\n",
       "f2                          0\n",
       "initial_list_status         0\n",
       "censor_status               0\n",
       "region                      0\n",
       "debt_loan_ratio             0\n",
       "scoring_high                0\n",
       "employer_type               0\n",
       "recircle_b                  0\n",
       "early_return_amount         0\n",
       "recircle_u                  0\n",
       "interest                    0\n",
       "industry                    0\n",
       "post_code                   0\n",
       "early_return                0\n",
       "year_of_loan                0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8eb8d772",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 特征衍生"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0e4d8e6",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 自定义衍生函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f31adfa9",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "def createfeature(df,colname):\n",
    "    middle_value=df.groupby(colname)['isDefault'].mean()\n",
    "    middle_dict=dict(zip(middle_value.index,middle_value))\n",
    "    col=\"{0}_DefaultRate\".format(colname)\n",
    "    df[col]=df[colname].map(middle_dict)\n",
    "    return df,middle_dict,col"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "018cacea",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 是否有房对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3beccc3b",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,house_dict,col=createfeature(Train_middle,\"house_exist\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "937b373e",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_middle[col]=Test_middle[\"house_exist\"].map(house_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6f4162a7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>7734.230769</td>\n",
       "      <td>9927</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>31329.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>18514.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>20707.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>14016.153850</td>\n",
       "      <td>0</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  employer_type    recircle_b  \\\n",
       "0  0.0               0.0  ...           政府机构   7734.230769   \n",
       "1  0.0               0.0  ...           政府机构  31329.000000   \n",
       "2  0.0               0.0  ...           政府机构  18514.000000   \n",
       "3  0.0               0.0  ...          世界五百强  20707.000000   \n",
       "4  0.0               0.0  ...           政府机构  14016.153850   \n",
       "\n",
       "   early_return_amount  recircle_u  interest         industry post_code  \\\n",
       "0                 9927   91.800000    11.466              金融业     193.0   \n",
       "1                    0   54.800000    16.841              金融业     491.0   \n",
       "2                    0   57.692308     8.900        公共服务、社会组织     459.0   \n",
       "3                    0   42.600000     4.788           文化和体育业     157.0   \n",
       "4                    0   30.461538    12.790  信息传输、软件和信息技术服务业      38.0   \n",
       "\n",
       "   early_return  year_of_loan  house_exist_DefaultRate  \n",
       "0             3             3                 0.171535  \n",
       "1             0             5                 0.171535  \n",
       "2             0             3                 0.231908  \n",
       "3             0             3                 0.171535  \n",
       "4             0             3                 0.207403  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8c8090b1",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>幼教与中小学校</td>\n",
       "      <td>8647.692308</td>\n",
       "      <td>773</td>\n",
       "      <td>31.846154</td>\n",
       "      <td>10.791</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>9406.153846</td>\n",
       "      <td>1894</td>\n",
       "      <td>18.276923</td>\n",
       "      <td>9.990</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>26414.769230</td>\n",
       "      <td>5670</td>\n",
       "      <td>62.300000</td>\n",
       "      <td>15.763</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>1198.000000</td>\n",
       "      <td>4800</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>19.305</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>3920.000000</td>\n",
       "      <td>3516</td>\n",
       "      <td>8.830769</td>\n",
       "      <td>7.139</td>\n",
       "      <td>金融业</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  employer_type    recircle_b  \\\n",
       "0  0.0               0.0  ...        幼教与中小学校   8647.692308   \n",
       "1  0.0               0.0  ...           普通企业   9406.153846   \n",
       "2  0.0               0.0  ...           普通企业  26414.769230   \n",
       "3  0.0               0.0  ...           普通企业   1198.000000   \n",
       "4  0.0               0.0  ...          世界五百强   3920.000000   \n",
       "\n",
       "   early_return_amount  recircle_u  interest industry  post_code  \\\n",
       "0                  773   31.846154    10.791   住宿和餐饮业        314   \n",
       "1                 1894   18.276923     9.990   批发和零售业         29   \n",
       "2                 5670   62.300000    15.763   住宿和餐饮业        488   \n",
       "3                 4800    7.700000    19.305      采矿业        489   \n",
       "4                 3516    8.830769     7.139      金融业        418   \n",
       "\n",
       "   early_return  year_of_loan  house_exist_DefaultRate  \n",
       "0             3             3                 0.171535  \n",
       "1             1             3                 0.231908  \n",
       "2             1             3                 0.171535  \n",
       "3             2             5                 0.171535  \n",
       "4             0             3                 0.171535  \n",
       "\n",
       "[5 rows x 36 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d8f8173",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 工作领域对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f607cce9",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,industry_dict,col=createfeature(Train_middle,\"industry\")\n",
    "Test_middle[col]=Test_middle[\"industry\"].map(industry_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "74926fc4",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>7734.230769</td>\n",
       "      <td>9927</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>31329.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18514.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>20707.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>14016.153850</td>\n",
       "      <td>0</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...    recircle_b  early_return_amount  recircle_u  \\\n",
       "0  0.0               0.0  ...   7734.230769                 9927   91.800000   \n",
       "1  0.0               0.0  ...  31329.000000                    0   54.800000   \n",
       "2  0.0               0.0  ...  18514.000000                    0   57.692308   \n",
       "3  0.0               0.0  ...  20707.000000                    0   42.600000   \n",
       "4  0.0               0.0  ...  14016.153850                    0   30.461538   \n",
       "\n",
       "   interest         industry  post_code early_return  year_of_loan  \\\n",
       "0    11.466              金融业      193.0            3             3   \n",
       "1    16.841              金融业      491.0            0             5   \n",
       "2     8.900        公共服务、社会组织      459.0            0             3   \n",
       "3     4.788           文化和体育业      157.0            0             3   \n",
       "4    12.790  信息传输、软件和信息技术服务业       38.0            0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  \n",
       "0                 0.171535              0.200263  \n",
       "1                 0.171535              0.200263  \n",
       "2                 0.231908              0.199465  \n",
       "3                 0.171535              0.201460  \n",
       "4                 0.207403              0.197855  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e1993265",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8647.692308</td>\n",
       "      <td>773</td>\n",
       "      <td>31.846154</td>\n",
       "      <td>10.791</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9406.153846</td>\n",
       "      <td>1894</td>\n",
       "      <td>18.276923</td>\n",
       "      <td>9.990</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>26414.769230</td>\n",
       "      <td>5670</td>\n",
       "      <td>62.300000</td>\n",
       "      <td>15.763</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1198.000000</td>\n",
       "      <td>4800</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>19.305</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3920.000000</td>\n",
       "      <td>3516</td>\n",
       "      <td>8.830769</td>\n",
       "      <td>7.139</td>\n",
       "      <td>金融业</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...    recircle_b  early_return_amount  recircle_u  \\\n",
       "0  0.0               0.0  ...   8647.692308                  773   31.846154   \n",
       "1  0.0               0.0  ...   9406.153846                 1894   18.276923   \n",
       "2  0.0               0.0  ...  26414.769230                 5670   62.300000   \n",
       "3  0.0               0.0  ...   1198.000000                 4800    7.700000   \n",
       "4  0.0               0.0  ...   3920.000000                 3516    8.830769   \n",
       "\n",
       "   interest  industry post_code  early_return  year_of_loan  \\\n",
       "0    10.791    住宿和餐饮业       314             3             3   \n",
       "1     9.990    批发和零售业        29             1             3   \n",
       "2    15.763    住宿和餐饮业       488             1             3   \n",
       "3    19.305       采矿业       489             2             5   \n",
       "4     7.139       金融业       418             0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  \n",
       "0                 0.171535              0.196281  \n",
       "1                 0.231908              0.194244  \n",
       "2                 0.171535              0.196281  \n",
       "3                 0.171535              0.201377  \n",
       "4                 0.171535              0.200263  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1991f314",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 工作年限对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "791a33fd",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,work_year_dict,col=createfeature(Train_middle,\"work_year\")\n",
    "Test_middle[col]=Test_middle[\"work_year\"].map(work_year_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0bcda31c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9927</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  early_return_amount  recircle_u  interest  \\\n",
       "0  0.0               0.0  ...                 9927   91.800000    11.466   \n",
       "1  0.0               0.0  ...                    0   54.800000    16.841   \n",
       "2  0.0               0.0  ...                    0   57.692308     8.900   \n",
       "3  0.0               0.0  ...                    0   42.600000     4.788   \n",
       "4  0.0               0.0  ...                    0   30.461538    12.790   \n",
       "\n",
       "          industry  post_code  early_return year_of_loan  \\\n",
       "0              金融业      193.0             3            3   \n",
       "1              金融业      491.0             0            5   \n",
       "2        公共服务、社会组织      459.0             0            3   \n",
       "3           文化和体育业      157.0             0            3   \n",
       "4  信息传输、软件和信息技术服务业       38.0             0            3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  work_year_DefaultRate  \n",
       "0                 0.171535              0.200263               0.201621  \n",
       "1                 0.171535              0.200263               0.199258  \n",
       "2                 0.231908              0.199465               0.199258  \n",
       "3                 0.171535              0.201460               0.192361  \n",
       "4                 0.207403              0.197855               0.204504  \n",
       "\n",
       "[5 rows x 39 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "738e37c7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>773</td>\n",
       "      <td>31.846154</td>\n",
       "      <td>10.791</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1894</td>\n",
       "      <td>18.276923</td>\n",
       "      <td>9.990</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5670</td>\n",
       "      <td>62.300000</td>\n",
       "      <td>15.763</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4800</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>19.305</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3516</td>\n",
       "      <td>8.830769</td>\n",
       "      <td>7.139</td>\n",
       "      <td>金融业</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  early_return_amount  recircle_u  interest  \\\n",
       "0  0.0               0.0  ...                  773   31.846154    10.791   \n",
       "1  0.0               0.0  ...                 1894   18.276923     9.990   \n",
       "2  0.0               0.0  ...                 5670   62.300000    15.763   \n",
       "3  0.0               0.0  ...                 4800    7.700000    19.305   \n",
       "4  0.0               0.0  ...                 3516    8.830769     7.139   \n",
       "\n",
       "   industry  post_code early_return  year_of_loan  house_exist_DefaultRate  \\\n",
       "0    住宿和餐饮业        314            3             3                 0.171535   \n",
       "1    批发和零售业         29            1             3                 0.231908   \n",
       "2    住宿和餐饮业        488            1             3                 0.171535   \n",
       "3       采矿业        489            2             5                 0.171535   \n",
       "4       金融业        418            0             3                 0.171535   \n",
       "\n",
       "   industry_DefaultRate  work_year_DefaultRate  \n",
       "0              0.196281               0.196586  \n",
       "1              0.194244               0.199258  \n",
       "2              0.196281               0.199258  \n",
       "3              0.201377               0.199258  \n",
       "4              0.200263               0.205203  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93c64da0",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 公司类型对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4eb6871c",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,employer_type_dict,col=createfeature(Train_middle,\"employer_type\")\n",
    "Test_middle[col]=Test_middle[\"employer_type\"].map(employer_type_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "de4ffd3b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>91.800000</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>54.800000</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>57.692308</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>42.600000</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>30.461538</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 40 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  recircle_u  interest         industry  \\\n",
       "0  0.0               0.0  ...   91.800000    11.466              金融业   \n",
       "1  0.0               0.0  ...   54.800000    16.841              金融业   \n",
       "2  0.0               0.0  ...   57.692308     8.900        公共服务、社会组织   \n",
       "3  0.0               0.0  ...   42.600000     4.788           文化和体育业   \n",
       "4  0.0               0.0  ...   30.461538    12.790  信息传输、软件和信息技术服务业   \n",
       "\n",
       "   post_code  early_return  year_of_loan house_exist_DefaultRate  \\\n",
       "0      193.0             3             3                0.171535   \n",
       "1      491.0             0             5                0.171535   \n",
       "2      459.0             0             3                0.231908   \n",
       "3      157.0             0             3                0.171535   \n",
       "4       38.0             0             3                0.207403   \n",
       "\n",
       "   industry_DefaultRate  work_year_DefaultRate  employer_type_DefaultRate  \n",
       "0              0.200263               0.201621                   0.071215  \n",
       "1              0.200263               0.199258                   0.071215  \n",
       "2              0.199465               0.199258                   0.071215  \n",
       "3              0.201460               0.192361                   0.110743  \n",
       "4              0.197855               0.204504                   0.071215  \n",
       "\n",
       "[5 rows x 40 columns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "0dcf252e",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>31.846154</td>\n",
       "      <td>10.791</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18.276923</td>\n",
       "      <td>9.990</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>62.300000</td>\n",
       "      <td>15.763</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>19.305</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.830769</td>\n",
       "      <td>7.139</td>\n",
       "      <td>金融业</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  recircle_u  interest  industry  post_code  \\\n",
       "0  0.0               0.0  ...   31.846154    10.791    住宿和餐饮业        314   \n",
       "1  0.0               0.0  ...   18.276923     9.990    批发和零售业         29   \n",
       "2  0.0               0.0  ...   62.300000    15.763    住宿和餐饮业        488   \n",
       "3  0.0               0.0  ...    7.700000    19.305       采矿业        489   \n",
       "4  0.0               0.0  ...    8.830769     7.139       金融业        418   \n",
       "\n",
       "   early_return year_of_loan  house_exist_DefaultRate  industry_DefaultRate  \\\n",
       "0             3            3                 0.171535              0.196281   \n",
       "1             1            3                 0.231908              0.194244   \n",
       "2             1            3                 0.171535              0.196281   \n",
       "3             2            5                 0.171535              0.201377   \n",
       "4             0            3                 0.171535              0.200263   \n",
       "\n",
       "   work_year_DefaultRate  employer_type_DefaultRate  \n",
       "0               0.196586                   0.196909  \n",
       "1               0.199258                   0.293899  \n",
       "2               0.199258                   0.293899  \n",
       "3               0.199258                   0.293899  \n",
       "4               0.205203                   0.110743  \n",
       "\n",
       "[5 rows x 39 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee48172e",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 贷款用途类别对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3c6d0033",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,use_dict,col=createfeature(Train_middle,\"use\")\n",
    "Test_middle[col]=Test_middle[\"use\"].map(use_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "64993b98",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>11.466</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>16.841</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.900</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.788</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>12.790</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  interest         industry  post_code  \\\n",
       "0  0.0               0.0  ...    11.466              金融业      193.0   \n",
       "1  0.0               0.0  ...    16.841              金融业      491.0   \n",
       "2  0.0               0.0  ...     8.900        公共服务、社会组织      459.0   \n",
       "3  0.0               0.0  ...     4.788           文化和体育业      157.0   \n",
       "4  0.0               0.0  ...    12.790  信息传输、软件和信息技术服务业       38.0   \n",
       "\n",
       "   early_return  year_of_loan  house_exist_DefaultRate industry_DefaultRate  \\\n",
       "0             3             3                 0.171535             0.200263   \n",
       "1             0             5                 0.171535             0.200263   \n",
       "2             0             3                 0.231908             0.199465   \n",
       "3             0             3                 0.171535             0.201460   \n",
       "4             0             3                 0.207403             0.197855   \n",
       "\n",
       "   work_year_DefaultRate  employer_type_DefaultRate  use_DefaultRate  \n",
       "0               0.201621                   0.071215         0.175933  \n",
       "1               0.199258                   0.071215         0.211087  \n",
       "2               0.199258                   0.071215         0.169267  \n",
       "3               0.192361                   0.110743         0.211087  \n",
       "4               0.204504                   0.071215         0.211087  \n",
       "\n",
       "[5 rows x 41 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "8fab4be1",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>interest</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>10.791</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.990</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>15.763</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>19.305</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>7.139</td>\n",
       "      <td>金融业</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 40 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  interest  industry  post_code  early_return  \\\n",
       "0  0.0               0.0  ...    10.791    住宿和餐饮业        314             3   \n",
       "1  0.0               0.0  ...     9.990    批发和零售业         29             1   \n",
       "2  0.0               0.0  ...    15.763    住宿和餐饮业        488             1   \n",
       "3  0.0               0.0  ...    19.305       采矿业        489             2   \n",
       "4  0.0               0.0  ...     7.139       金融业        418             0   \n",
       "\n",
       "   year_of_loan house_exist_DefaultRate  industry_DefaultRate  \\\n",
       "0             3                0.171535              0.196281   \n",
       "1             3                0.231908              0.194244   \n",
       "2             3                0.171535              0.196281   \n",
       "3             5                0.171535              0.201377   \n",
       "4             3                0.171535              0.200263   \n",
       "\n",
       "   work_year_DefaultRate  employer_type_DefaultRate  use_DefaultRate  \n",
       "0               0.196586                   0.196909         0.211087  \n",
       "1               0.199258                   0.293899         0.210072  \n",
       "2               0.199258                   0.293899         0.211087  \n",
       "3               0.199258                   0.293899         0.211087  \n",
       "4               0.205203                   0.110743         0.175933  \n",
       "\n",
       "[5 rows x 40 columns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89ac307e",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 邮政编码对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "a990cb75",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,post_code_dict,col=createfeature(Train_middle,\"post_code\")\n",
    "Test_middle[col]=Test_middle[\"post_code\"].map(post_code_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "48d9ab7c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>industry</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>金融业</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>金融业</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...         industry  post_code  early_return  \\\n",
       "0  0.0               0.0  ...              金融业      193.0             3   \n",
       "1  0.0               0.0  ...              金融业      491.0             0   \n",
       "2  0.0               0.0  ...        公共服务、社会组织      459.0             0   \n",
       "3  0.0               0.0  ...           文化和体育业      157.0             0   \n",
       "4  0.0               0.0  ...  信息传输、软件和信息技术服务业       38.0             0   \n",
       "\n",
       "   year_of_loan  house_exist_DefaultRate  industry_DefaultRate  \\\n",
       "0             3                 0.171535              0.200263   \n",
       "1             5                 0.171535              0.200263   \n",
       "2             3                 0.231908              0.199465   \n",
       "3             3                 0.171535              0.201460   \n",
       "4             3                 0.207403              0.197855   \n",
       "\n",
       "  work_year_DefaultRate  employer_type_DefaultRate  use_DefaultRate  \\\n",
       "0              0.201621                   0.071215         0.175933   \n",
       "1              0.199258                   0.071215         0.211087   \n",
       "2              0.199258                   0.071215         0.169267   \n",
       "3              0.192361                   0.110743         0.211087   \n",
       "4              0.204504                   0.071215         0.211087   \n",
       "\n",
       "   post_code_DefaultRate  \n",
       "0               0.239884  \n",
       "1               0.217284  \n",
       "2               0.154179  \n",
       "3               0.190991  \n",
       "4               0.229373  \n",
       "\n",
       "[5 rows x 42 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "f6ca2d84",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "      <th>scoring_avg</th>\n",
       "      <th>censor_status_0</th>\n",
       "      <th>censor_status_1</th>\n",
       "      <th>censor_status_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>807.083333</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "      <td>710.227273</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>2013</td>\n",
       "      <td>4</td>\n",
       "      <td>852.954545</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>596.590909</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "      <td>664.772727</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  post_code_DefaultRate  region_DefaultRate  \\\n",
       "0  0.0               0.0  ...               0.191149            0.207120   \n",
       "1  0.0               0.0  ...               0.220657            0.207490   \n",
       "2  0.0               0.0  ...               0.220225            0.212236   \n",
       "3  0.0               0.0  ...               0.252226            0.210798   \n",
       "4  0.0               0.0  ...               0.168696            0.169376   \n",
       "\n",
       "   title_DefaultRate  class_DefaultRate  issue_date_y issue_date_m  \\\n",
       "0           0.217822           0.191149          2017           12   \n",
       "1           0.215275           0.220657          2015            7   \n",
       "2           0.116633           0.220225          2013            4   \n",
       "3           0.217822           0.252226          2017           12   \n",
       "4           0.182422           0.168696          2016            6   \n",
       "\n",
       "   scoring_avg  censor_status_0  censor_status_1  censor_status_2  \n",
       "0   807.083333                0                1                0  \n",
       "1   710.227273                0                1                0  \n",
       "2   852.954545                0                0                1  \n",
       "3   596.590909                0                0                1  \n",
       "4   664.772727                1                0                0  \n",
       "\n",
       "[5 rows x 50 columns]"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b63ecf9",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 地区对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "483504a6",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,region_dict,col=createfeature(Train_middle,\"region\")\n",
    "Test_middle[col]=Test_middle[\"region\"].map(region_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "665cc8e7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>491.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>459.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>157.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>38.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  post_code  early_return  year_of_loan  \\\n",
       "0  0.0               0.0  ...      193.0             3             3   \n",
       "1  0.0               0.0  ...      491.0             0             5   \n",
       "2  0.0               0.0  ...      459.0             0             3   \n",
       "3  0.0               0.0  ...      157.0             0             3   \n",
       "4  0.0               0.0  ...       38.0             0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  work_year_DefaultRate  \\\n",
       "0                 0.171535              0.200263               0.201621   \n",
       "1                 0.171535              0.200263               0.199258   \n",
       "2                 0.231908              0.199465               0.199258   \n",
       "3                 0.171535              0.201460               0.192361   \n",
       "4                 0.207403              0.197855               0.204504   \n",
       "\n",
       "  employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                  0.071215         0.175933               0.239884   \n",
       "1                  0.071215         0.211087               0.217284   \n",
       "2                  0.071215         0.169267               0.154179   \n",
       "3                  0.110743         0.211087               0.190991   \n",
       "4                  0.071215         0.211087               0.229373   \n",
       "\n",
       "   region_DefaultRate  \n",
       "0            0.219372  \n",
       "1            0.210798  \n",
       "2            0.194737  \n",
       "3            0.194737  \n",
       "4            0.215028  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "5a58638e",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>post_code</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>314</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>488</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>489</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>418</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  post_code  early_return  year_of_loan  \\\n",
       "0  0.0               0.0  ...        314             3             3   \n",
       "1  0.0               0.0  ...         29             1             3   \n",
       "2  0.0               0.0  ...        488             1             3   \n",
       "3  0.0               0.0  ...        489             2             5   \n",
       "4  0.0               0.0  ...        418             0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate work_year_DefaultRate  \\\n",
       "0                 0.171535              0.196281              0.196586   \n",
       "1                 0.231908              0.194244              0.199258   \n",
       "2                 0.171535              0.196281              0.199258   \n",
       "3                 0.171535              0.201377              0.199258   \n",
       "4                 0.171535              0.200263              0.205203   \n",
       "\n",
       "   employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.196909         0.211087               0.191149   \n",
       "1                   0.293899         0.210072               0.220657   \n",
       "2                   0.293899         0.211087               0.220225   \n",
       "3                   0.293899         0.211087               0.252226   \n",
       "4                   0.110743         0.175933               0.168696   \n",
       "\n",
       "   region_DefaultRate  \n",
       "0            0.207120  \n",
       "1            0.207490  \n",
       "2            0.212236  \n",
       "3            0.210798  \n",
       "4            0.169376  \n",
       "\n",
       "[5 rows x 42 columns]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "845243d8",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 提供贷款名称对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2f7f0284",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,title_dict,col=createfeature(Train_middle,\"title\")\n",
    "Test_middle[col]=Test_middle[\"title\"].map(title_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "5f807145",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "      <td>0.182422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.178082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.217822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "      <td>0.217822</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 44 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  early_return  year_of_loan  \\\n",
       "0  0.0               0.0  ...             3             3   \n",
       "1  0.0               0.0  ...             0             5   \n",
       "2  0.0               0.0  ...             0             3   \n",
       "3  0.0               0.0  ...             0             3   \n",
       "4  0.0               0.0  ...             0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  work_year_DefaultRate  \\\n",
       "0                 0.171535              0.200263               0.201621   \n",
       "1                 0.171535              0.200263               0.199258   \n",
       "2                 0.231908              0.199465               0.199258   \n",
       "3                 0.171535              0.201460               0.192361   \n",
       "4                 0.207403              0.197855               0.204504   \n",
       "\n",
       "   employer_type_DefaultRate use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.071215        0.175933               0.239884   \n",
       "1                   0.071215        0.211087               0.217284   \n",
       "2                   0.071215        0.169267               0.154179   \n",
       "3                   0.110743        0.211087               0.190991   \n",
       "4                   0.071215        0.211087               0.229373   \n",
       "\n",
       "   region_DefaultRate  title_DefaultRate  \n",
       "0            0.219372           0.182422  \n",
       "1            0.210798           0.000000  \n",
       "2            0.194737           0.178082  \n",
       "3            0.194737           0.217822  \n",
       "4            0.215028           0.217822  \n",
       "\n",
       "[5 rows x 44 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "243ceec3",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>early_return</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  early_return  year_of_loan  \\\n",
       "0  0.0               0.0  ...             3             3   \n",
       "1  0.0               0.0  ...             1             3   \n",
       "2  0.0               0.0  ...             1             3   \n",
       "3  0.0               0.0  ...             2             5   \n",
       "4  0.0               0.0  ...             0             3   \n",
       "\n",
       "   house_exist_DefaultRate  industry_DefaultRate  work_year_DefaultRate  \\\n",
       "0                 0.171535              0.196281               0.196586   \n",
       "1                 0.231908              0.194244               0.199258   \n",
       "2                 0.171535              0.196281               0.199258   \n",
       "3                 0.171535              0.201377               0.199258   \n",
       "4                 0.171535              0.200263               0.205203   \n",
       "\n",
       "  employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                  0.196909         0.211087               0.191149   \n",
       "1                  0.293899         0.210072               0.220657   \n",
       "2                  0.293899         0.211087               0.220225   \n",
       "3                  0.293899         0.211087               0.252226   \n",
       "4                  0.110743         0.175933               0.168696   \n",
       "\n",
       "   region_DefaultRate  title_DefaultRate  \n",
       "0            0.207120           0.217822  \n",
       "1            0.207490           0.215275  \n",
       "2            0.212236           0.116633  \n",
       "3            0.210798           0.217822  \n",
       "4            0.169376           0.182422  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fd2d7b6",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 贷款级别对应违约率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "e51de368",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle,class_dict,col=createfeature(Train_middle,\"class\")\n",
    "Test_middle[col]=Test_middle[\"class\"].map(class_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "060d6d1f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.224656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.224656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.178082</td>\n",
       "      <td>0.060462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.060462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.207403</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.224656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 45 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  year_of_loan  house_exist_DefaultRate  \\\n",
       "0  0.0               0.0  ...             3                 0.171535   \n",
       "1  0.0               0.0  ...             5                 0.171535   \n",
       "2  0.0               0.0  ...             3                 0.231908   \n",
       "3  0.0               0.0  ...             3                 0.171535   \n",
       "4  0.0               0.0  ...             3                 0.207403   \n",
       "\n",
       "   industry_DefaultRate  work_year_DefaultRate  employer_type_DefaultRate  \\\n",
       "0              0.200263               0.201621                   0.071215   \n",
       "1              0.200263               0.199258                   0.071215   \n",
       "2              0.199465               0.199258                   0.071215   \n",
       "3              0.201460               0.192361                   0.110743   \n",
       "4              0.197855               0.204504                   0.071215   \n",
       "\n",
       "   use_DefaultRate post_code_DefaultRate  region_DefaultRate  \\\n",
       "0         0.175933              0.239884            0.219372   \n",
       "1         0.211087              0.217284            0.210798   \n",
       "2         0.169267              0.154179            0.194737   \n",
       "3         0.211087              0.190991            0.194737   \n",
       "4         0.211087              0.229373            0.215028   \n",
       "\n",
       "   title_DefaultRate  class_DefaultRate  \n",
       "0           0.182422           0.224656  \n",
       "1           0.000000           0.224656  \n",
       "2           0.178082           0.060462  \n",
       "3           0.217822           0.060462  \n",
       "4           0.217822           0.224656  \n",
       "\n",
       "[5 rows x 45 columns]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "0759d1f7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>house_exist_DefaultRate</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.132797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.231908</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "      <td>0.132797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "      <td>0.224656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.304049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>0.171535</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.060462</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 44 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  year_of_loan  house_exist_DefaultRate  \\\n",
       "0  0.0               0.0  ...             3                 0.171535   \n",
       "1  0.0               0.0  ...             3                 0.231908   \n",
       "2  0.0               0.0  ...             3                 0.171535   \n",
       "3  0.0               0.0  ...             5                 0.171535   \n",
       "4  0.0               0.0  ...             3                 0.171535   \n",
       "\n",
       "   industry_DefaultRate  work_year_DefaultRate  employer_type_DefaultRate  \\\n",
       "0              0.196281               0.196586                   0.196909   \n",
       "1              0.194244               0.199258                   0.293899   \n",
       "2              0.196281               0.199258                   0.293899   \n",
       "3              0.201377               0.199258                   0.293899   \n",
       "4              0.200263               0.205203                   0.110743   \n",
       "\n",
       "  use_DefaultRate  post_code_DefaultRate  region_DefaultRate  \\\n",
       "0        0.211087               0.191149            0.207120   \n",
       "1        0.210072               0.220657            0.207490   \n",
       "2        0.211087               0.220225            0.212236   \n",
       "3        0.211087               0.252226            0.210798   \n",
       "4        0.175933               0.168696            0.169376   \n",
       "\n",
       "   title_DefaultRate  class_DefaultRate  \n",
       "0           0.217822           0.132797  \n",
       "1           0.215275           0.132797  \n",
       "2           0.116633           0.224656  \n",
       "3           0.217822           0.304049  \n",
       "4           0.182422           0.060462  \n",
       "\n",
       "[5 rows x 44 columns]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e98c9f80",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 计算贷款年份&月份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "7c5bf85b",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#转化为时间格式\n",
    "Train_middle['issue_date']=pd.to_datetime(Train_middle['issue_date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "c9eb3d1a",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#计算年份\n",
    "Train_middle[\"issue_date_y\"]=Train_middle['issue_date'].dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "475701db",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#计算月份\n",
    "Train_middle[\"issue_date_m\"]=Train_middle['issue_date'].dt.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "6de4ce56",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199465</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.178082</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2014</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.201460</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.197855</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  industry_DefaultRate  work_year_DefaultRate  \\\n",
       "0  0.0               0.0  ...              0.200263               0.201621   \n",
       "1  0.0               0.0  ...              0.200263               0.199258   \n",
       "2  0.0               0.0  ...              0.199465               0.199258   \n",
       "3  0.0               0.0  ...              0.201460               0.192361   \n",
       "4  0.0               0.0  ...              0.197855               0.204504   \n",
       "\n",
       "   employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.071215         0.175933               0.239884   \n",
       "1                   0.071215         0.211087               0.217284   \n",
       "2                   0.071215         0.169267               0.154179   \n",
       "3                   0.110743         0.211087               0.190991   \n",
       "4                   0.071215         0.211087               0.229373   \n",
       "\n",
       "   region_DefaultRate title_DefaultRate  class_DefaultRate  issue_date_y  \\\n",
       "0            0.219372          0.182422           0.224656          2016   \n",
       "1            0.210798          0.000000           0.224656          2013   \n",
       "2            0.194737          0.178082           0.060462          2014   \n",
       "3            0.194737          0.217822           0.060462          2015   \n",
       "4            0.215028          0.217822           0.224656          2016   \n",
       "\n",
       "   issue_date_m  \n",
       "0            10  \n",
       "1             6  \n",
       "2             1  \n",
       "3             7  \n",
       "4             7  \n",
       "\n",
       "[5 rows x 47 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "a23f29c2",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_middle['issue_date']=pd.to_datetime(Test_middle['issue_date'])\n",
    "Test_middle[\"issue_date_y\"]=Test_middle['issue_date'].dt.year\n",
    "Test_middle[\"issue_date_m\"]=Test_middle['issue_date'].dt.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "7ab710c0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>industry_DefaultRate</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.194244</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.196281</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.201377</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.304049</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200263</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 46 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  industry_DefaultRate  work_year_DefaultRate  \\\n",
       "0  0.0               0.0  ...              0.196281               0.196586   \n",
       "1  0.0               0.0  ...              0.194244               0.199258   \n",
       "2  0.0               0.0  ...              0.196281               0.199258   \n",
       "3  0.0               0.0  ...              0.201377               0.199258   \n",
       "4  0.0               0.0  ...              0.200263               0.205203   \n",
       "\n",
       "   employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.196909         0.211087               0.191149   \n",
       "1                   0.293899         0.210072               0.220657   \n",
       "2                   0.293899         0.211087               0.220225   \n",
       "3                   0.293899         0.211087               0.252226   \n",
       "4                   0.110743         0.175933               0.168696   \n",
       "\n",
       "  region_DefaultRate  title_DefaultRate  class_DefaultRate  issue_date_y  \\\n",
       "0           0.207120           0.217822           0.132797          2017   \n",
       "1           0.207490           0.215275           0.132797          2015   \n",
       "2           0.212236           0.116633           0.224656          2013   \n",
       "3           0.210798           0.217822           0.304049          2017   \n",
       "4           0.169376           0.182422           0.060462          2016   \n",
       "\n",
       "   issue_date_m  \n",
       "0            12  \n",
       "1             7  \n",
       "2             4  \n",
       "3            12  \n",
       "4             6  \n",
       "\n",
       "[5 rows x 46 columns]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "108e9608",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 计算贷款评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "a214101c",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle[\"scoring_avg\"]=(Train_middle[\"scoring_high\"]+Train_middle[\"scoring_low\"])/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "ad097149",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_middle[\"scoring_avg\"]=(Test_middle[\"scoring_high\"]+Test_middle[\"scoring_low\"])/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "2f246f55",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "      <th>scoring_avg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.201621</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>10</td>\n",
       "      <td>602.727273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>6</td>\n",
       "      <td>804.375000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.169267</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.178082</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2014</td>\n",
       "      <td>1</td>\n",
       "      <td>839.090909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.192361</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "      <td>812.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.204504</td>\n",
       "      <td>0.071215</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>7</td>\n",
       "      <td>659.848485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 48 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  work_year_DefaultRate  \\\n",
       "0  0.0               0.0  ...               0.201621   \n",
       "1  0.0               0.0  ...               0.199258   \n",
       "2  0.0               0.0  ...               0.199258   \n",
       "3  0.0               0.0  ...               0.192361   \n",
       "4  0.0               0.0  ...               0.204504   \n",
       "\n",
       "   employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.071215         0.175933               0.239884   \n",
       "1                   0.071215         0.211087               0.217284   \n",
       "2                   0.071215         0.169267               0.154179   \n",
       "3                   0.110743         0.211087               0.190991   \n",
       "4                   0.071215         0.211087               0.229373   \n",
       "\n",
       "   region_DefaultRate  title_DefaultRate class_DefaultRate  issue_date_y  \\\n",
       "0            0.219372           0.182422          0.224656          2016   \n",
       "1            0.210798           0.000000          0.224656          2013   \n",
       "2            0.194737           0.178082          0.060462          2014   \n",
       "3            0.194737           0.217822          0.060462          2015   \n",
       "4            0.215028           0.217822          0.224656          2016   \n",
       "\n",
       "   issue_date_m  scoring_avg  \n",
       "0            10   602.727273  \n",
       "1             6   804.375000  \n",
       "2             1   839.090909  \n",
       "3             7   812.500000  \n",
       "4             7   659.848485  \n",
       "\n",
       "[5 rows x 48 columns]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "546da9c8",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>work_year_DefaultRate</th>\n",
       "      <th>employer_type_DefaultRate</th>\n",
       "      <th>use_DefaultRate</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "      <th>scoring_avg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.196586</td>\n",
       "      <td>0.196909</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>807.083333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.210072</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "      <td>710.227273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>4</td>\n",
       "      <td>852.954545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199258</td>\n",
       "      <td>0.293899</td>\n",
       "      <td>0.211087</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.304049</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>596.590909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.205203</td>\n",
       "      <td>0.110743</td>\n",
       "      <td>0.175933</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "      <td>664.772727</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  work_year_DefaultRate  \\\n",
       "0  0.0               0.0  ...               0.196586   \n",
       "1  0.0               0.0  ...               0.199258   \n",
       "2  0.0               0.0  ...               0.199258   \n",
       "3  0.0               0.0  ...               0.199258   \n",
       "4  0.0               0.0  ...               0.205203   \n",
       "\n",
       "   employer_type_DefaultRate  use_DefaultRate  post_code_DefaultRate  \\\n",
       "0                   0.196909         0.211087               0.191149   \n",
       "1                   0.293899         0.210072               0.220657   \n",
       "2                   0.293899         0.211087               0.220225   \n",
       "3                   0.293899         0.211087               0.252226   \n",
       "4                   0.110743         0.175933               0.168696   \n",
       "\n",
       "   region_DefaultRate title_DefaultRate  class_DefaultRate  issue_date_y  \\\n",
       "0            0.207120          0.217822           0.132797          2017   \n",
       "1            0.207490          0.215275           0.132797          2015   \n",
       "2            0.212236          0.116633           0.224656          2013   \n",
       "3            0.210798          0.217822           0.304049          2017   \n",
       "4            0.169376          0.182422           0.060462          2016   \n",
       "\n",
       "   issue_date_m  scoring_avg  \n",
       "0            12   807.083333  \n",
       "1             7   710.227273  \n",
       "2             4   852.954545  \n",
       "3            12   596.590909  \n",
       "4             6   664.772727  \n",
       "\n",
       "[5 rows x 47 columns]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e95a9b8",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 特征转码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ff8639a",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 审核情况转换哑变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "3b8150a7",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "censor_dict={0:\"censor_status_0\",1:\"censor_status_1\",2:\"censor_status_2\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "fd1d4420",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "censor_dummies=pd.get_dummies(Train_middle[\"censor_status\"]).rename(columns=censor_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "e2c3464f",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Train_middle=pd.concat((Train_middle,censor_dummies),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "dacdfc9f",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "censor_dummies=pd.get_dummies(Test_middle[\"censor_status\"]).rename(columns=censor_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "a53167cc",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "Test_middle=pd.concat((Test_middle,censor_dummies),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "18da19c5",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "      <th>scoring_avg</th>\n",
       "      <th>censor_status_0</th>\n",
       "      <th>censor_status_1</th>\n",
       "      <th>censor_status_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3 years</td>\n",
       "      <td>5.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.239884</td>\n",
       "      <td>0.219372</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>10</td>\n",
       "      <td>602.727273</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>40642.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.217284</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>6</td>\n",
       "      <td>804.375000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>154.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.154179</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.178082</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2014</td>\n",
       "      <td>1</td>\n",
       "      <td>839.090909</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0.0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.190991</td>\n",
       "      <td>0.194737</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "      <td>812.500000</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>0.0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.229373</td>\n",
       "      <td>0.215028</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2016</td>\n",
       "      <td>7</td>\n",
       "      <td>659.848485</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 51 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon    f0  work_year    title class  \\\n",
       "0   5.0    2          1.0              1-Dec   1.0    3 years      5.0     C   \n",
       "1  45.0    0          1.0             Apr-90   7.0  10+ years  40642.0     C   \n",
       "2  28.0    4          1.0             Oct-91   6.0  10+ years    154.0     A   \n",
       "3  15.0    0          1.0              1-Jun   5.0    6 years      0.0     A   \n",
       "4  15.0    0          1.0              2-May  10.0   < 1 year      0.0     C   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  post_code_DefaultRate  region_DefaultRate  \\\n",
       "0  0.0               0.0  ...               0.239884            0.219372   \n",
       "1  0.0               0.0  ...               0.217284            0.210798   \n",
       "2  0.0               0.0  ...               0.154179            0.194737   \n",
       "3  0.0               0.0  ...               0.190991            0.194737   \n",
       "4  0.0               0.0  ...               0.229373            0.215028   \n",
       "\n",
       "   title_DefaultRate  class_DefaultRate  issue_date_y  issue_date_m  \\\n",
       "0           0.182422           0.224656          2016            10   \n",
       "1           0.000000           0.224656          2013             6   \n",
       "2           0.178082           0.060462          2014             1   \n",
       "3           0.217822           0.060462          2015             7   \n",
       "4           0.217822           0.224656          2016             7   \n",
       "\n",
       "  scoring_avg  censor_status_0  censor_status_1  censor_status_2  \n",
       "0  602.727273                0                1                0  \n",
       "1  804.375000                0                0                1  \n",
       "2  839.090909                1                0                0  \n",
       "3  812.500000                0                1                0  \n",
       "4  659.848485                0                1                0  \n",
       "\n",
       "[5 rows x 51 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train_middle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "5c3c293b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>f3</th>\n",
       "      <th>use</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>f0</th>\n",
       "      <th>work_year</th>\n",
       "      <th>title</th>\n",
       "      <th>class</th>\n",
       "      <th>f1</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>...</th>\n",
       "      <th>post_code_DefaultRate</th>\n",
       "      <th>region_DefaultRate</th>\n",
       "      <th>title_DefaultRate</th>\n",
       "      <th>class_DefaultRate</th>\n",
       "      <th>issue_date_y</th>\n",
       "      <th>issue_date_m</th>\n",
       "      <th>scoring_avg</th>\n",
       "      <th>censor_status_0</th>\n",
       "      <th>censor_status_1</th>\n",
       "      <th>censor_status_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.191149</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>807.083333</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>29.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>6</td>\n",
       "      <td>B</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.220657</td>\n",
       "      <td>0.207490</td>\n",
       "      <td>0.215275</td>\n",
       "      <td>0.132797</td>\n",
       "      <td>2015</td>\n",
       "      <td>7</td>\n",
       "      <td>710.227273</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>268</td>\n",
       "      <td>C</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.220225</td>\n",
       "      <td>0.212236</td>\n",
       "      <td>0.116633</td>\n",
       "      <td>0.224656</td>\n",
       "      <td>2013</td>\n",
       "      <td>4</td>\n",
       "      <td>852.954545</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.252226</td>\n",
       "      <td>0.210798</td>\n",
       "      <td>0.217822</td>\n",
       "      <td>0.304049</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "      <td>596.590909</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1 year</td>\n",
       "      <td>5</td>\n",
       "      <td>A</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.168696</td>\n",
       "      <td>0.169376</td>\n",
       "      <td>0.182422</td>\n",
       "      <td>0.060462</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "      <td>664.772727</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     f3  use  policy_code earlies_credit_mon   f0  work_year  title class  \\\n",
       "0   5.0    0            1              3-Mar  2.0    5 years      0     B   \n",
       "1  29.0    5            1             Dec-99  8.0  10+ years      6     B   \n",
       "2  10.0    0            1             Apr-99  6.0  10+ years    268     C   \n",
       "3  10.0    0            1             Jul-00  4.0  10+ years      0     D   \n",
       "4  14.0    2            1              7-May  4.0     1 year      5     A   \n",
       "\n",
       "    f1  pub_dero_bankrup  ...  post_code_DefaultRate  region_DefaultRate  \\\n",
       "0  0.0               0.0  ...               0.191149            0.207120   \n",
       "1  0.0               0.0  ...               0.220657            0.207490   \n",
       "2  0.0               0.0  ...               0.220225            0.212236   \n",
       "3  0.0               0.0  ...               0.252226            0.210798   \n",
       "4  0.0               0.0  ...               0.168696            0.169376   \n",
       "\n",
       "   title_DefaultRate  class_DefaultRate  issue_date_y issue_date_m  \\\n",
       "0           0.217822           0.132797          2017           12   \n",
       "1           0.215275           0.132797          2015            7   \n",
       "2           0.116633           0.224656          2013            4   \n",
       "3           0.217822           0.304049          2017           12   \n",
       "4           0.182422           0.060462          2016            6   \n",
       "\n",
       "   scoring_avg  censor_status_0  censor_status_1  censor_status_2  \n",
       "0   807.083333                0                1                0  \n",
       "1   710.227273                0                1                0  \n",
       "2   852.954545                0                0                1  \n",
       "3   596.590909                0                0                1  \n",
       "4   664.772727                1                0                0  \n",
       "\n",
       "[5 rows x 50 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Test_middle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "677488d1",
   "metadata": {},
   "source": [
    "## 删除不必要列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "246196b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "del_cols=[\"user_id\",\"house_exist\",\"censor_status\",\"industry\",\"work_year\",\"employer_type\",\n",
    "          \"loan_id\",\"use\",\"issue_date\",\"earlies_credit_mon\",\"post_code\",\"region\",\"title\",\"policy_code\",\"class\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "dc4376e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data_befor=Train_middle.drop(columns=del_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "6cabcb9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_befor=Test_middle.drop(columns=del_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "e6a80029",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_befor.fillna(0,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e4dc7cc",
   "metadata": {},
   "source": [
    "# 失衡数据判断及处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "cc534c70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.800596\n",
       "1    0.199404\n",
       "Name: isDefault, dtype: float64"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data_befor[\"isDefault\"].value_counts()/train_data_befor.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "5e9c9a86",
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据为失衡数据，且违约数据占比20%，达151293个，可以使用欠采样\n",
    "y_sample=train_data_befor.pop(\"isDefault\")\n",
    "x_sample=train_data_befor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "7a433d83",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.under_sampling import NearMiss\n",
    "NM=NearMiss(version=1)\n",
    "x_resample,y_resample=NM.fit_resample(x_sample,y_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "1663d0a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    151293\n",
       "1    151293\n",
       "Name: isDefault, dtype: int64"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat((x_resample,y_resample),axis=1)[\"isDefault\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6270262b",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "1b9bf8c6",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "2455426c",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "xtrain_data,xtest_data,ytrain_data,ytest_data=train_test_split(x_resample,y_resample,test_size=0.3,random_state=420)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b70730f",
   "metadata": {},
   "source": [
    "# 多个备选模型并比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "02b1539a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "20da4bb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def model_select(xtrain,ytrain,xtest,ytest,model_name,model):\n",
    "    print(\"训练{0}模型\".format(model_name))\n",
    "    start=time.time()\n",
    "    clf=model\n",
    "    clf=clf.fit(xtrain,ytrain)\n",
    "    \n",
    "    #训练集拟合度\n",
    "    ytrain_pre=clf.predict(xtrain)\n",
    "    train_AUC=metrics.roc_auc_score(ytrain,ytrain_pre)\n",
    "    train_recall=metrics.recall_score(ytrain,ytrain_pre)\n",
    "    print(\"训练集AUC为：{:.3f}\".format(train_AUC))\n",
    "    print(\"训练集召回率为：{:.3f}\".format(train_recall))\n",
    "    \n",
    "    #测试集拟合度\n",
    "    ytest_pre=clf.predict(xtest)\n",
    "    test_AUC=metrics.roc_auc_score(ytest,ytest_pre)\n",
    "    test_recall=metrics.recall_score(ytest,ytest_pre)\n",
    "    test_precision=metrics.precision_score(ytest,ytest_pre)\n",
    "    print(\"测试集AUC为：{:.3f}\".format(test_AUC))\n",
    "    print(\"测试集召回率为：{:.3f}\".format(test_recall))\n",
    "    print(\"测试集精准率为：{:.3f}\".format(test_precision))\n",
    "    \n",
    "    #模型耗时\n",
    "    end=time.time()\n",
    "    duration=end-start\n",
    "    print(\"模型耗时：{:6f}s\".format(duration))\n",
    "    \n",
    "    return model_name,train_AUC,train_recall,test_AUC,test_recall,test_precision,duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "3f998732",
   "metadata": {},
   "outputs": [],
   "source": [
    "#常规模型\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "#集成模型\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "72513289",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练决策树模型\n",
      "训练集AUC为：1.000\n",
      "训练集召回率为：1.000\n",
      "测试集AUC为：0.727\n",
      "测试集召回率为：0.726\n",
      "测试集精准率为：0.727\n",
      "模型耗时：6.422798s\n",
      "训练逻辑回归模型\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\mi\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集AUC为：0.707\n",
      "训练集召回率为：0.658\n",
      "测试集AUC为：0.706\n",
      "测试集召回率为：0.660\n",
      "测试集精准率为：0.727\n",
      "模型耗时：3.228980s\n",
      "训练随机森林模型\n",
      "训练集AUC为：1.000\n",
      "训练集召回率为：1.000\n",
      "测试集AUC为：0.799\n",
      "测试集召回率为：0.758\n",
      "测试集精准率为：0.827\n",
      "模型耗时：69.210106s\n",
      "训练Adaboost模型\n",
      "训练集AUC为：0.785\n",
      "训练集召回率为：0.747\n",
      "测试集AUC为：0.785\n",
      "测试集召回率为：0.749\n",
      "测试集精准率为：0.808\n",
      "模型耗时：19.320618s\n",
      "训练Gradient模型\n",
      "训练集AUC为：0.800\n",
      "训练集召回率为：0.765\n",
      "测试集AUC为：0.799\n",
      "测试集召回率为：0.765\n",
      "测试集精准率为：0.820\n",
      "模型耗时：77.240688s\n"
     ]
    }
   ],
   "source": [
    "model_params={\n",
    "    \"决策树\":DecisionTreeClassifier(),\n",
    "    \"逻辑回归\":LogisticRegression(),\n",
    "    \"随机森林\":RandomForestClassifier(),\n",
    "    \"Adaboost\":AdaBoostClassifier(),\n",
    "    \"Gradient\":GradientBoostingClassifier()\n",
    "}\n",
    "\n",
    "result_data=pd.DataFrame(columns=[\"模型名称\",\"训练集AUC\",\"训练集召回率\",\"测试集AUC\",\n",
    "                                  \"测试集召回率\",\"测试集精准率\",\"模型耗时\"])\n",
    "\n",
    "for model_name,model in model_params.items():\n",
    "    result=model_select(xtrain_data,ytrain_data,xtest_data,ytest_data,model_name,model)\n",
    "    result_data.loc[result_data.shape[0],:]=result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "9ac9c6f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>模型名称</th>\n",
       "      <th>训练集AUC</th>\n",
       "      <th>训练集召回率</th>\n",
       "      <th>测试集AUC</th>\n",
       "      <th>测试集召回率</th>\n",
       "      <th>测试集精准率</th>\n",
       "      <th>模型耗时</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>决策树</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.727053</td>\n",
       "      <td>0.726079</td>\n",
       "      <td>0.727409</td>\n",
       "      <td>6.422798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>逻辑回归</td>\n",
       "      <td>0.706572</td>\n",
       "      <td>0.658094</td>\n",
       "      <td>0.705991</td>\n",
       "      <td>0.660078</td>\n",
       "      <td>0.726732</td>\n",
       "      <td>3.22898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>随机森林</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.799464</td>\n",
       "      <td>0.757636</td>\n",
       "      <td>0.82674</td>\n",
       "      <td>69.210106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Adaboost</td>\n",
       "      <td>0.784957</td>\n",
       "      <td>0.747099</td>\n",
       "      <td>0.785475</td>\n",
       "      <td>0.748512</td>\n",
       "      <td>0.808195</td>\n",
       "      <td>19.320618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Gradient</td>\n",
       "      <td>0.799673</td>\n",
       "      <td>0.765114</td>\n",
       "      <td>0.798695</td>\n",
       "      <td>0.765128</td>\n",
       "      <td>0.820125</td>\n",
       "      <td>77.240688</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       模型名称    训练集AUC    训练集召回率    测试集AUC    测试集召回率    测试集精准率       模型耗时\n",
       "0       决策树       1.0       1.0  0.727053  0.726079  0.727409   6.422798\n",
       "1      逻辑回归  0.706572  0.658094  0.705991  0.660078  0.726732    3.22898\n",
       "2      随机森林       1.0       1.0  0.799464  0.757636   0.82674  69.210106\n",
       "3  Adaboost  0.784957  0.747099  0.785475  0.748512  0.808195  19.320618\n",
       "4  Gradient  0.799673  0.765114  0.798695  0.765128  0.820125  77.240688"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e848db7",
   "metadata": {},
   "source": [
    "比较发现随机森林无论是测试机AUC还是召回率都比其它模型好，所以使用随机森林模型，并进行调优"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a5eabbc",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 网格优化进行模型调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "9759dd8e",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#由于原来的模型AUC已经较高了，所以在原基础上进行调优\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "clf=RandomForestClassifier()\n",
    "clf=clf.fit(xtrain_data,ytrain_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "7916fd3c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gini'"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.criterion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "5cf25537",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.n_estimators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "5af553de",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(xtrain_data.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "0955ea31",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100, 110, 120, 130, 140, 150])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(100,160,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "5f8b63ea",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10.0, 15.0, 20.0, 25.0, 30.0, 35.0]"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(np.linspace(10,35,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "e0ae2af3",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\mi\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\sklearn\\model_selection\\_search.py:922: UserWarning: One or more of the test scores are non-finite: [nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan\n",
      " nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan\n",
      " nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan\n",
      " nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=RandomForestClassifier(), n_jobs=2,\n",
       "             param_grid={'criterion': ['gini', 'entropy'],\n",
       "                         'max_depth': [10.0, 15.0, 20.0, 25.0, 30.0, 35.0],\n",
       "                         'n_estimators': array([100, 110, 120, 130, 140, 150])},\n",
       "             scoring=<function roc_auc_score at 0x000001A00C6AFF70>)"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_params={\n",
    "    \"criterion\":[\"gini\",\"entropy\"],\n",
    "    \"n_estimators\":np.arange(100,160,10),\n",
    "    \"max_depth\":list(np.linspace(10,35,6))\n",
    "}\n",
    "clf=RandomForestClassifier()\n",
    "GV=GridSearchCV(clf,p_params,cv=5,n_jobs=2,scoring=metrics.roc_auc_score)\n",
    "GV.fit(xtrain_data,ytrain_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "8eebc8ba",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'criterion': 'gini', 'max_depth': 10.0, 'n_estimators': 100}"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GV.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "445f1c41",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "model=RandomForestClassifier(max_depth=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "8653a229",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(max_depth=10)"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(xtrain_data,ytrain_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "25c8093c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.780345942383706"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.roc_auc_score(ytest_data,model.predict(xtest_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "77bc119f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7990126030855284"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.roc_auc_score(ytest_data,clf.predict(xtest_data))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78aa37f3",
   "metadata": {
    "hidden": true
   },
   "source": [
    "通过比较，决定还是使用随机森林的默认参数进行分析"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0800602",
   "metadata": {},
   "source": [
    "# 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "7d0bfe4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "34c3f6f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['个贷违约预测模型']"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf,\"个贷违约预测模型\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7fa31e4",
   "metadata": {},
   "source": [
    "# 比赛预测并保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "abaadcc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "ytest_pre_befor=clf.predict(test_data_befor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "693b7370",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5000, 1)"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ytest_pre_befor.reshape(-1,1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "97eb2b64",
   "metadata": {},
   "outputs": [],
   "source": [
    "Test_predict=pd.DataFrame({\"id\":Test_middle[\"loan_id\"],\"isDefault\":ytest_pre_befor})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "f1a2bf12",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.ExcelWriter('C:/Users/MI/OneDrive/桌面/Test_predict.xlsx') as writer:\n",
    "    Test_predict.to_excel(writer,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "663d0386",
   "metadata": {},
   "outputs": [],
   "source": [
    "Test_predict.to_csv('C:/Users/MI/OneDrive/桌面/Test_predict.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12400b27",
   "metadata": {},
   "outputs": [],
   "source": [
    "id, isDefault"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
